<?php
namespace App\Http\Controllers\Api\Wx;

use Illuminate\Http\Request;

use App\Service\WdhService;

use App\Models\Customer\Customer;

use App\Models\Wx\WxCustomerCard;

use App\Models\Wx\WxCustomerCardMap;

use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Redis as Redis;

class WxCustomerCardController extends Controller
{

    /**
     * 检查微信会员卡领取
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function check(Request $request)
    {

        $session_id = $request->input('session_id', session_id());
        $platform = $request->input('platform', 6);

        if ( empty( $session_id ) ) {
            return response()->json([ 'code' => 100001 , 'message' => '缺少身份标识' ]);
        }

        $pcustID = get_customer_id($session_id, $platform);
        if (!$pcustID) {
            return response()->json([ 'code' => 100002 , 'message' => '会员未登录' ]);
        }

        //查询用户是否已领取卡券
        $wx_customer_card_map = WxCustomerCardMap::select('cardId', 'cardCode', 'stat')->where('pcustID', $pcustID)->first();
        if (!$wx_customer_card_map || $wx_customer_card_map->stat == 2) {

            //获取微信会员卡
            $wx_customer_card = WxCustomerCard::select('wechatCardId')->where(['cardStatus' => 1])->first();
            if ( empty($wx_customer_card) ) {
                return response()->json([ 'code' => 100003 , 'message' => '会员卡不存在' ]);
            }

            //获取会员卡号
            $customer = Customer::select('cardNo')->find( $pcustID );
            if (!$customer) {
                return response()->json([ 'code' => 100004 , 'message' => '会员不存在' ]);
            }

            //获取微信api ticket
            $ticket = redisTransformation(Redis::get('WXCARD_API_TICKET'));
            if ( !$ticket ) {
                $ticket  = WdhService::getWxTicket();
                if ( !$ticket ) {
                    return response()->json([ 'code' => 100005 , 'message' => '微信Ticket错误' ]);
                }
            }

            //微信小程序粉丝信息
            $wx_applet_info = json_decode(Redis::get('WX_APPLET_USER_' . $session_id), true);
            if (!$wx_applet_info) {
                return response()->json([ 'code' => 100006 , 'message' => '小程序身份信息不存在' ]);
            }

            $openId = $wx_applet_info['openId'];
            $card_id = $wx_customer_card->wechatCardId;
            $code = $customer->cardNo;
            $nonce_str = makeUuid();
            $timestamp = time();
            $card = [$ticket, $timestamp, $card_id, $code, $nonce_str, $openId];
            sort( $card, SORT_STRING ) ;
            $signature =  sha1( implode( $card ) );

            $return_data = [
                'receive' => 0,
                'cardId' => $card_id,
                'code' => $code,
                'timestamp' => $timestamp,
                'nonce_str' => $nonce_str,
                'signature' => $signature,
            ];

        } else {
            $return_data = [
                'receive' => 1,
                'cardId' => $wx_customer_card_map->cardId,
                'code' => $wx_customer_card_map->cardCode,
            ];
        }

        return response()->json([ 'code' => 200 , 'message' => 'ok', 'data' => $return_data ]);

    }

}